package com.hyzs.utils;

import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "es")
public class EsConnUtils {


    private String url;
    private int port;
    private final Logger logger = LoggerFactory.getLogger(EsConnUtils.class);

    private RestHighLevelClient client;

    public static int MAX_SEARCH_LENGTH = 1000;

    public RestHighLevelClient getClient() {
        if(client == null)
           client = new RestHighLevelClient(RestClient.builder(
                new HttpHost(url, port, "http")));
        return client;
    }

    public void closeClient() {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getUrl() {
        return url;
    }


    public void setUrl(String url) {
        this.url = url;
    }

    public int getPort() {
        return port;
    }


    public void setPort(int port) {
        this.port = port;
    }
}
